#!/sw/bin/perl -w 

use strict;

use Tk;
use Tk::Adjuster;
use Tk::Dialog;
use Tk::HList;
use Tk::LabEntry;
use Tk::NoteBook;

use YAML;
use XML::Simple;

use Thread;

my @server = YAML::LoadFile( 'config.yaml' );

sub initialize_adjuster;
sub initialize_dialogs;
sub initialize_input;
sub initialize_list;
sub initialize_note_book;
sub initialize_menus;

sub gen_tab;

sub gui_update;
sub send_message_to_gui;


my $shared_text: shared = "";		
my $shared_flag: shared =  0;	

my $canvas;
my %dialogs = ( about => '', usage => '' );
my $adjuster;
my $menu_button_frame;

my $input;
my $text;
my $hlist;
my $book;

my $main_window = new MainWindow();

my $list;
my $txt;

&initialize_dialogs;
&initialize_menus;
&initialize_list;

&initialize_note_book;
&initialize_input;

#$txt->repeat(300, \&gui_update) ;
MainLoop;


sub initialize_input {
  $input = $main_window->Entry()->pack( -side => 'bottom',
                                        -fill => 'both',
                                        -expand => 1, );
}
  
#  $Inputfield = $lframe->Entry()->pack(-side   => 'left',
#                                       -fill   => 'x',
#                                       -expand => 1,
#                                       -pady   => 2);
#  $Inputfield->bind("<Return>", \&Say_Click);
#  $Inputfield->bind("<Control-Return>", \&Msg_Click);
#  $Inputfield->bind("<Tab>", \&completeName);




sub initialize_note_book {
  
  my $int = 1;
  
  $book = $main_window->NoteBook();
  for ( @server ) {
    gen_tab( $book->add( "Sheet ".$int++, -label => $_->{ host } ) );
    
    for my $x( @{ $_->{ channels }} ) {
      gen_tab( $book->add( "Sheet ".$int++, -label => $x ) );
    }
  }
  $book->pack( -fill => 'both', -expand => 1, -side => 'top'  );
}

sub gen_tab {
  my $tab = shift;
  my $adj = $tab->Adjuster( )->pack( $hlist, -side => 'left', -expand => 1, -fill => 'y' );
  $adj->Scrolled( "Text", -scrollbars => 'e' )->pack( -expand => 1, -side => 'left', -fill => 'both' );
}
  


sub initialize_list {
  
  $hlist = $main_window->Scrolled(
                                  'HList',
                                  -itemtype    => 'text',
                                  -separator   => '-',
                                  -selectmode  => 'single',
                                  -browsecmd   => sub { print "FOO\n" },
                                  -scrollbars  => 'osoe'
                                 );

  for ( @server ) {
    $hlist->add( $_->{ host }, -text => $_->{ host } );
  
    for my $x( @{ $_->{ channels }} ) {
      $hlist->add( $_->{ host } . '-' . $x, => -text => $x );
    }
  }
  
  $hlist->pack;#(-side => 'left', -fill => 'both', -expand => 1);
}


sub initialize_dialogs {
  
  
  $dialogs{ about } = $main_window->Dialog( 
                                           -title => 'about', 
                                           -text => "Version 0.01",
                                           -bitmap => 'info',
                                           -buttons => ['Dismiss'],
                                          );
  $dialogs{ about }->configure( -wraplength => '6i' );
  
  $dialogs{ usage } = $main_window->Dialog( 
                                           -title => 'Usage',
                                           -text => "Not useable right now." ,
                                           -buttons => ['Dismiss'],
                                          );
}


sub initialize_menus {

  $menu_button_frame = $main_window->Frame( 
                                           -relief => 'raised',
                                           -borderwidth => 1
                                           );
  $menu_button_frame->pack( -fill => 'x' );
  
  make_menubutton( $menu_button_frame, 'File', 0, 'left', 
                   [
                    ['Quit', [$main_window => 'exit'], 0]                              
                   ]
                 );
  make_menubutton( $menu_button_frame, 'Help', 0, 'left', 
                   [
                    [ 'About', [ $dialogs{ about } => 'Show' ], 0],
                    ['', undef, 0],
                    ['Usage', [ $dialogs{ usage } => 'Show' ], 0]
                   ]
                 );
}


sub make_menubutton {
	  
  my($mb_frame, $mb_label, $mb_label_underline, $pack, $mb_list_ref) = @_;
  
  my $mb = $mb_frame->Menubutton(
                            -text      => $mb_label, 
                            -underline => $mb_label_underline,
                           );
  my $mb_list;
  foreach $mb_list (@{$mb_list_ref}) {
    $mb_list->[0] eq '' ? $mb->separator :
      $mb->command(
                   -label     => $mb_list->[0], 
                   -command   => $mb_list->[1], 
                   -underline => $mb_list->[2],
                  );
  }
  $mb->pack(-side => $pack);
  
} # end make_menubutton



sub send_message_to_gui {
  my $msg = shift;
  
  while (1 == $shared_flag) {
    select(undef, undef, undef, 0.30);
  }
  
  $shared_text = $msg;  
  $shared_flag = 1;
}

sub gui_update {
  return if not $shared_flag;
  
  my $msg = $shared_text;
  
  if ($msg) {
    $txt->insert('end', $msg);
    $main_window->update();
  }
  
  $shared_flag = 0;
}
